package concurrent.forkjoin;

import java.sql.Connection;
import java.util.Arrays;
import java.util.Collection;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName MergeTest.java
 * @Description TODO
 * @createTime 2021年04月14日 22:39:00
 */
public class MergeTest {
    private static int MAX = 1000;
    private static int inits[] = new int[MAX];

    static{
        Random random = new Random();
        for (int i = 1; i <MAX ; i++) {
            inits[i-1] = random.nextInt(1000000);
        }
    }

    public static void main(String[] args) {
        long beginTime = System.currentTimeMillis();
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        MyTask myTask = new MyTask(inits);
        ForkJoinTask<int[]> taskResult = forkJoinPool.submit(myTask);

        try {
            int[] ints = taskResult.get();
            System.out.println(Arrays.toString(ints));
            System.out.println(ints.length);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        long entTime = System.currentTimeMillis();

        System.out.println("耗时="+(entTime-beginTime));

     /*   for (int i = 0; i <MAX; i++) {

            for (int j = 0; j < MAX-i-1; j++) {

                if (inits[j]>inits[j+1]){
                    int temp = inits[j+1];
                    inits[j+1] = inits[j];
                    inits[j] = temp;
                }
            }

        }*/
//        Arrays.sort(inits);
        System.out.println(Arrays.toString(inits));
        long san = System.currentTimeMillis();
        System.out.println("冒泡耗时="+(san-entTime));

    }

    static class MyTask extends RecursiveTask<int[]>{
         private int source[];

         public MyTask(int source[]){
             this.source = source;
         }
        @Override
        protected int[] compute() {
             int sourceLen = source.length;
             //如果条件成立，说明任务重要进行排序的集合还不够小
             if (sourceLen>2){
                 int midIndex = sourceLen/2;
                 //拆分成两个子任务
                 MyTask task1 = new MyTask(Arrays.copyOf(source,midIndex));
                 task1.fork();
                 MyTask task2 = new MyTask(Arrays.copyOfRange(source,midIndex,sourceLen));
                 task2.fork();
                 //将两个有序的数组，合并成一个有序的数组
                 int[] result1 = task1.join();
                 int[] result2 = task2.join();

                int mer[] = joinInts(result1,result2);
                return mer;
             }else {
                 if (sourceLen==1||source[0]<=source[1]){
                     return source;
                 }else {
                     int targetp[] = new int[sourceLen];
                     targetp[0] = source[1];
                     targetp[1] = source[0];
                     return targetp;
                 }
             }

        }
        /**
         * 合并两个有序集合
         */
        private  static int[] joinInts(int array1[],int array2[]){

            int sum = array1.length+array2.length;
            int [] newArr = new int[sum];
            for (int i = 0; i<sum ; ++i) {
              if (i<array1.length){
                  newArr[i] = array1[i];
              }
              if (i>=array1.length){
                  newArr[i] = array2[i-array1.length];
              }
            }
            Arrays.sort(newArr);
            return newArr;
        }


    }

}
